window.LangMessage = {__unload: true}
function jsonpcallback (result) {
  result = result || {}
  window.LangMessage = result.value || {}
}

initLang()

function initLang () {
  var lang
  navigator.language = navigator.language || navigator.browserLanguage
  lang = 'en_US'
  setCookie('lang', lang)
  setLangSrc()
}

function setLangSrc () { // 这里有隐患 异步
  var currentLang = getCookie('lang')
  var src = '/guojihua/i18n/resource/download.json?namespace=common,anan_portal_front&callback=jsonpcallback&lang=' + currentLang
  document.getElementById('langTag').src = src
}

function getCookie (name) {
  var cookie = document.cookie,
    cookieName = encodeURIComponent(name) + '=',
    start = cookie.indexOf(cookieName),
    value = null

  if (start > -1) {
    var cookieEnd = document.cookie.indexOf(';', start)
    if (cookieEnd == -1) {
      cookieEnd = document.cookie.length
    }
    value = decodeURIComponent(document.cookie.substring(start + cookieName.length, cookieEnd))
  }

  return value
}

function setCookie (name, value, param) {
  var text = encodeURIComponent(name) + '=' + encodeURIComponent(value)
  param = param || {}
  param.path = param.path || '/'
  var expires = param.expires
  if (expires instanceof Date) {
    text += ';expires=' + expires.toGMTString()
  } else if (typeof expires === 'number') {
    text += ';expires=' + new Date(expires).toGMTString()
  }
  if (param.path) {
    text += ';path=' + param.path
  }
  if (param.domain) {
    text += ';domain=' + param.domain
  }
  if (param.secure) {
    text += ';secure'
  }
  document.cookie = text
}
